SlideShare a Scribd company logo
Class No.33  Data Structures http://ecomputernotes.com
Searching an Array: Binary Search Binary search is like looking up a phone number or a word in the dictionary Start in middle of book If name you're looking for comes before names on page, look in first half Otherwise, look in second half http://ecomputernotes.com
Binary Search If ( value == middle element )  value is found  else if ( value < middle element )  search left-half of list with the same method  else  search right-half of list with the same method http://ecomputernotes.com
Case 1:  val == a[mid] val = 10 low = 0, high = 8 5 7 9 10 13 17 19 1 27 1 2 3 4 5 6 7 0 8 a: low high Binary Search mid mid = (0 + 8) / 2 = 4 10 http://ecomputernotes.com
Case 2:  val > a[mid] val = 19 low = 0, high = 8 mid = (0 + 8) / 2 = 4  Binary Search -- Example 2 5 7 9 10 1 13 17 19 27 1 2 3 4 5 6 7 0 8 a: mid low high new low new low = mid+1 = 5 13 17 19 27 http://ecomputernotes.com
Case 3:  val < a[mid] val = 7 low = 0, high = 8 mid = (0 + 8) / 2 = 4  Binary Search -- Example 3 10 13 17 19 5 7 9 1 27 1 2 3 4 5 6 7 0 8 a: mid low high new high new high = mid-1 = 3 5 7 9 1 http://ecomputernotes.com
val = 7 Binary Search -- Example 3 (cont) 5 7 9 10 13 17 19 1 27 1 2 3 4 5 6 7 0 8 a: 5 7 9 10 13 17 19 1 27 1 2 3 4 5 6 7 0 8 a: 5 7 9 10 13 17 19 1 27 1 2 3 4 5 6 7 0 8 a:
Binary Search – C++ Code int isPresent(int *arr, int val, int N) { int low = 0; int high = N - 1; int mid; while ( low <= high ){ mid = ( low + high )/2; if (arr[mid]== val) return 1; // found! else if (arr[mid] < val) low = mid + 1; else high = mid - 1; } return 0; // not found }
Binary Search: binary tree The search divides a list into two small sub-lists till a sub-list is no more divisible. First half First half An entire sorted list First half Second half Second half http://ecomputernotes.com
Binary Search Efficiency After 1 bisection N/2 items After 2 bisections N/4 = N/2 2  items   . . .  After  i  bisections N/2 i  = 1 item i  = log 2  N http://ecomputernotes.com
Implementation 3: linked list TableNodes are again stored consecutively (unsorted or sorted) insert : add to front; (1 or n for a sorted list ) find : search through potentially all the keys, one at a time; ( n   for unsorted or for a sorted list remove : find, remove using pointer alterations; ( n ) key entry and so on http://ecomputernotes.com
Implementation 4: Skip List Overcome basic limitations of previous lists Search and update require linear time Fast Searching of Sorted Chain  Provide alternative to BST (binary search trees) and related tree structures. Balancing can be expensive. Relatively recent data structure: Bill Pugh proposed it in 1990. http://ecomputernotes.com
Skip List Representation Can do better than  n  comparisons to find element in chain of length  n 20 30 40 50 60 head tail http://ecomputernotes.com
Skip List Representation Example:  n/2 + 1  if we keep pointer to middle element 20 30 40 50 60 head tail http://ecomputernotes.com
Higher Level Chains For general n, level 0 chain includes all elements level 1 every other element, level 2 chain every fourth, etc. level  i , every  2 i   th element 40 50 60 head tail 20 30 26 57 level 1&2 chains http://ecomputernotes.com
Higher Level Chains Skip list contains a hierarchy of chains In general level  i  contains a subset of elements in level  i-1 40 50 60 head tail 20 30 26 57 level 1&2 chains http://ecomputernotes.com
Skip List: formally A skip list for a set  S  of distinct (key, element) items is a series of lists  S 0 ,  S 1  , … ,  S h  such that Each list  S i  contains the special keys  and   List  S 0  contains the keys of  S  in nondecreasing order  Each list is a subsequence of the previous one, i.e., S 0     S 1     …   S h List  S h  contains only the two special keys

More Related Content

PPTX
Doubly linked list
PPTX
Double link list
PPTX
Double Linked List (Algorithm)
PPTX
linked list
PPT
computer notes - Data Structures - 32
PPT
Computer notes - Binary Search
PPTX
Doubly Linked List
PPT
Unit ii(dsc++)
Doubly linked list
Double link list
Double Linked List (Algorithm)
linked list
computer notes - Data Structures - 32
Computer notes - Binary Search
Doubly Linked List
Unit ii(dsc++)

What's hot (20)

PDF
Doubly Link List
PPTX
Doubly Linked Lists
PPTX
PPTX
Linked list
PPT
Link list
PPTX
DOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMAR
PPT
Singly link list
PPTX
Linked List
PPT
Circular linked list
PPT
Data Structure and Algorithms Linked List
PPSX
Data Structure (Circular Linked List)
PPT
Linked list
PDF
PPTX
Linked list
PPT
linked list
PDF
Circular linked list
PPTX
Deletion from single way linked list and search
PPT
linked list
PPTX
Ppt on Linked list,stack,queue
PPTX
Linked list
Doubly Link List
Doubly Linked Lists
Linked list
Link list
DOUBLE LINKED LIST(DATA STRUCTURE) PPT BY PRASUN KUMAR
Singly link list
Linked List
Circular linked list
Data Structure and Algorithms Linked List
Data Structure (Circular Linked List)
Linked list
Linked list
linked list
Circular linked list
Deletion from single way linked list and search
linked list
Ppt on Linked list,stack,queue
Linked list
Ad

Viewers also liked (20)

PPT
computer notes - Data Structures - 21
PPT
computer notes - Data Structures - 2
PPT
computer notes - Data Structures - 25
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 39
PPT
computer notes - Data Structures - 8
PPT
computer notes - Data Structures - 37
PPT
computer notes - Data Structures - 36
PPT
computer notes - Data Structures - 20
PPT
computer notes - Data Structures - 4
PPT
computer notes - Data Structures - 12
PPT
computer notes - Data Structures - 14
PPT
computer notes - Data Structures - 18
PPT
computer notes - Data Structures - 1
PPT
computer notes - Data Structures - 5
PPT
computer notes - Data Structures - 29
PPT
computer notes - Data Structures - 7
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 27
PPT
computer notes - Data Structures - 6
computer notes - Data Structures - 21
computer notes - Data Structures - 2
computer notes - Data Structures - 25
computer notes - Data Structures - 19
computer notes - Data Structures - 39
computer notes - Data Structures - 8
computer notes - Data Structures - 37
computer notes - Data Structures - 36
computer notes - Data Structures - 20
computer notes - Data Structures - 4
computer notes - Data Structures - 12
computer notes - Data Structures - 14
computer notes - Data Structures - 18
computer notes - Data Structures - 1
computer notes - Data Structures - 5
computer notes - Data Structures - 29
computer notes - Data Structures - 7
computer notes - Data Structures - 28
computer notes - Data Structures - 27
computer notes - Data Structures - 6
Ad

Similar to computer notes - Data Structures - 33 (20)

PPT
C Language Unit-6
PDF
advanced searching and sorting.pdf
PPT
Unit6 jwfiles
PDF
Basics in algorithms and data structure
PPT
Fundamentals of data structures
PPT
Data Structures- Part3 arrays and searching algorithms
PDF
Data structures arrays
DOCX
MODULE 5-Searching and-sorting
PPT
Algorithm
PPT
search_sort.ppt
DOCX
UNIT V.docx
PPTX
Data structures
PPT
Computer notes - Mergesort
PDF
Searching
PPTX
21CS32 DS Module 1 PPT.pptx
PPT
computer notes - Data Structures - 34
PPTX
data_structure_Chapter two_computer.pptx
PPTX
Searching and Sorting Techniques (DSA).pptx
PPTX
project on data structures and algorithm
C Language Unit-6
advanced searching and sorting.pdf
Unit6 jwfiles
Basics in algorithms and data structure
Fundamentals of data structures
Data Structures- Part3 arrays and searching algorithms
Data structures arrays
MODULE 5-Searching and-sorting
Algorithm
search_sort.ppt
UNIT V.docx
Data structures
Computer notes - Mergesort
Searching
21CS32 DS Module 1 PPT.pptx
computer notes - Data Structures - 34
data_structure_Chapter two_computer.pptx
Searching and Sorting Techniques (DSA).pptx
project on data structures and algorithm

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 15
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 13
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 16
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 35
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 10
DOC
Computer notes - Controlling User Access
DOC
Computer notes - Using SET Operator
computer notes - Data Structures - 30
computer notes - Data Structures - 11
computer notes - Data Structures - 15
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 31
computer notes - Data Structures - 13
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 16
computer notes - Data Structures - 22
computer notes - Data Structures - 35
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 10
Computer notes - Controlling User Access
Computer notes - Using SET Operator

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Spectroscopy.pptx food analysis technology
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
KodekX | Application Modernization Development
Per capita expenditure prediction using model stacking based on satellite ima...
sap open course for s4hana steps from ECC to s4
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation theory and applications.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
MIND Revenue Release Quarter 2 2025 Press Release
Machine learning based COVID-19 study performance prediction
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
NewMind AI Weekly Chronicles - August'25 Week I
Spectroscopy.pptx food analysis technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Dropbox Q2 2025 Financial Results & Investor Presentation
Diabetes mellitus diagnosis method based random forest with bat algorithm
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
The AUB Centre for AI in Media Proposal.docx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
cuic standard and advanced reporting.pdf
KodekX | Application Modernization Development

computer notes - Data Structures - 33

  • 1. Class No.33 Data Structures http://ecomputernotes.com
  • 2. Searching an Array: Binary Search Binary search is like looking up a phone number or a word in the dictionary Start in middle of book If name you're looking for comes before names on page, look in first half Otherwise, look in second half http://ecomputernotes.com
  • 3. Binary Search If ( value == middle element ) value is found else if ( value < middle element ) search left-half of list with the same method else search right-half of list with the same method http://ecomputernotes.com
  • 4. Case 1: val == a[mid] val = 10 low = 0, high = 8 5 7 9 10 13 17 19 1 27 1 2 3 4 5 6 7 0 8 a: low high Binary Search mid mid = (0 + 8) / 2 = 4 10 http://ecomputernotes.com
  • 5. Case 2: val > a[mid] val = 19 low = 0, high = 8 mid = (0 + 8) / 2 = 4 Binary Search -- Example 2 5 7 9 10 1 13 17 19 27 1 2 3 4 5 6 7 0 8 a: mid low high new low new low = mid+1 = 5 13 17 19 27 http://ecomputernotes.com
  • 6. Case 3: val < a[mid] val = 7 low = 0, high = 8 mid = (0 + 8) / 2 = 4 Binary Search -- Example 3 10 13 17 19 5 7 9 1 27 1 2 3 4 5 6 7 0 8 a: mid low high new high new high = mid-1 = 3 5 7 9 1 http://ecomputernotes.com
  • 7. val = 7 Binary Search -- Example 3 (cont) 5 7 9 10 13 17 19 1 27 1 2 3 4 5 6 7 0 8 a: 5 7 9 10 13 17 19 1 27 1 2 3 4 5 6 7 0 8 a: 5 7 9 10 13 17 19 1 27 1 2 3 4 5 6 7 0 8 a:
  • 8. Binary Search – C++ Code int isPresent(int *arr, int val, int N) { int low = 0; int high = N - 1; int mid; while ( low <= high ){ mid = ( low + high )/2; if (arr[mid]== val) return 1; // found! else if (arr[mid] < val) low = mid + 1; else high = mid - 1; } return 0; // not found }
  • 9. Binary Search: binary tree The search divides a list into two small sub-lists till a sub-list is no more divisible. First half First half An entire sorted list First half Second half Second half http://ecomputernotes.com
  • 10. Binary Search Efficiency After 1 bisection N/2 items After 2 bisections N/4 = N/2 2 items . . . After i bisections N/2 i = 1 item i = log 2 N http://ecomputernotes.com
  • 11. Implementation 3: linked list TableNodes are again stored consecutively (unsorted or sorted) insert : add to front; (1 or n for a sorted list ) find : search through potentially all the keys, one at a time; ( n for unsorted or for a sorted list remove : find, remove using pointer alterations; ( n ) key entry and so on http://ecomputernotes.com
  • 12. Implementation 4: Skip List Overcome basic limitations of previous lists Search and update require linear time Fast Searching of Sorted Chain Provide alternative to BST (binary search trees) and related tree structures. Balancing can be expensive. Relatively recent data structure: Bill Pugh proposed it in 1990. http://ecomputernotes.com
  • 13. Skip List Representation Can do better than n comparisons to find element in chain of length n 20 30 40 50 60 head tail http://ecomputernotes.com
  • 14. Skip List Representation Example: n/2 + 1 if we keep pointer to middle element 20 30 40 50 60 head tail http://ecomputernotes.com
  • 15. Higher Level Chains For general n, level 0 chain includes all elements level 1 every other element, level 2 chain every fourth, etc. level i , every 2 i th element 40 50 60 head tail 20 30 26 57 level 1&2 chains http://ecomputernotes.com
  • 16. Higher Level Chains Skip list contains a hierarchy of chains In general level i contains a subset of elements in level i-1 40 50 60 head tail 20 30 26 57 level 1&2 chains http://ecomputernotes.com
  • 17. Skip List: formally A skip list for a set S of distinct (key, element) items is a series of lists S 0 , S 1 , … , S h such that Each list S i contains the special keys  and  List S 0 contains the keys of S in nondecreasing order Each list is a subsequence of the previous one, i.e., S 0  S 1  …  S h List S h contains only the two special keys

Editor's Notes

  • #3: End of Lecture 38
  • #4: Start lecture 39
  • #18: End of lecture 39, Start of lecture 40.